package job

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/model"
	"github.com/wagslane/go-rabbitmq"
	"log"
	"time"
)

type consumer struct {
}

func NewConsumer() *consumer {
	return &consumer{}
}

func (c *consumer) OrderC() {

	conn, err := NewRabbitMQConn(mqUrl, rabbitmq.WithConnectionOptionsLogging)

	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	consumer, err := NewRabbitMQConsumer(conn, "my_yiis",
		rabbitmq.WithConsumerOptionsRoutingKey("routing_yii"),
		rabbitmq.WithConsumerOptionsExchangeName("Yii"),
		rabbitmq.WithConsumerOptionsExchangeDeclare)
	if err != nil {
		log.Fatal(err)
	}
	defer consumer.Close()

	err = consumer.Run(func(d rabbitmq.Delivery) rabbitmq.Action {
		data := string(d.Body)
		if data == "13053011580" {
			time.Sleep(30 * time.Second)
			adminUser := new(model.AdminUser)
			whereSql := "id = ?"
			where := make([]interface{}, 0)
			where = append(where, 2)

			model.NewDb("").UpdateRecord(adminUser, whereSql, where, map[string]interface{}{"username": "luyong_yiran" + libs.Random(6, true)})
		}

		//rabbitmq.Ack, rabbitmq.NackDiscard, rabbitmq.NackRequeue
		return rabbitmq.Ack
	})
	if err != nil {
		log.Fatal(err)
	}
}
